﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

using AimGameLibrary;
using AimGameLibrary.GameObject;

namespace AimGameEngine.EditorObject
{
    public enum Syco
    {
        Input,
        Output
    }

    public class Connecter : Image2D
    {
        public Rectangle NodeBody;
        public Vector2 PositionToCenter { get { return MyViewData.Position + new Vector2(NodeBody.Width / 2, NodeBody.Height / 2); } }
        public Point PositionBodyCenter { get { return new Point(NodeBody.X + (NodeBody.Width / 2), NodeBody.Y + (NodeBody.Height / 2)); } }
        //public LinkLine linkline;
        public Syco MySyco;
        public NodeTypeValue MyType;
        public NodeObject MyNode;

        public Connecter(SpriteBatch inputspritebatch, Camera inputcamera, Syco MySyco, NodeTypeValue MyType, NodeObject MyNode)
            : base(inputspritebatch, inputcamera)
        {
            this.MySyco = MySyco;
            this.MyType = MyType;
            //this.Scale = new Vector2(10, 10);
            this.MyNode = MyNode;
            MyViewData.Colors = Color.Green;
        }

        public override void SetupSourceAndTexture(Texture2D inputTexture)
        {
            base.SetupSourceAndTexture(inputTexture);
            if (MyViewData.Source.Width <= 1 && MyViewData.Source.Height <= 1)
                MyViewData.Source = new Rectangle(0, 0, 10, 10);
            this.NodeBody = MyViewData.Source;
        }

        public void SetupPosition(Vector2 inputPosition, Vector2 inputsize)
        {
            if (MySyco == Syco.Output)
            {
                if (MyNode.ListConnectpointOutput.Count == 1)
                    MyViewData.Position = inputPosition + new Vector2(inputsize.X / 2, -8);
                else if (MyNode.ListConnectpointOutput.Count % 2 == 0)
                    MyViewData.Position = inputPosition + GetEvenPosition(new Vector2(inputsize.X / 2, -8));
                else MyViewData.Position = inputPosition + GetOddPosition(new Vector2(inputsize.X / 2, -8));
            }
            else if (MySyco == Syco.Input)
            {
                if (MyNode.ListConnectpointInput.Count == 1)
                    MyViewData.Position = inputPosition + new Vector2(inputsize.X / 2, inputsize.Y - 2);
                else if (MyNode.ListConnectpointInput.Count % 2 == 0)
                    MyViewData.Position = inputPosition + GetEvenPosition(new Vector2(inputsize.X / 2, inputsize.Y - 2));
                else MyViewData.Position = inputPosition + GetOddPosition(new Vector2(inputsize.X / 2, inputsize.Y - 2));
            }

        }

        Vector2 GetEvenPosition(Vector2 inputsize)
        {
            float TempCount;
            if (MySyco == Syco.Input)
                TempCount = MyNode.ListConnectpointInput.Count;
            else TempCount = MyNode.ListConnectpointOutput.Count;
            float HalfCount = TempCount / 2;
            float StartPosition = HalfCount * 15;
            float TempIndex;
            if (MySyco == Syco.Input)
                TempIndex = MyNode.ListConnectpointInput.IndexOf(this);
            else TempIndex = MyNode.ListConnectpointOutput.IndexOf(this);
            float PositionX = (inputsize.X - StartPosition) + (TempIndex * 15);
            float PositionY = inputsize.Y;
            return new Vector2(PositionX, PositionY);
        }

        Vector2 GetOddPosition(Vector2 inputsize)
        {
            float TempCount;
            if (MySyco == Syco.Input)
                TempCount = MyNode.ListConnectpointInput.Count;
            else TempCount = MyNode.ListConnectpointOutput.Count;
            float HalfCount = TempCount / 2;
            float StartPosition = HalfCount * 15;
            float count = (MyNode.ListConnectpoint.Count - 1) / 2;
            float TempIndex;
            if (MySyco == Syco.Input)
                TempIndex = MyNode.ListConnectpointInput.IndexOf(this);
            else TempIndex = MyNode.ListConnectpointOutput.IndexOf(this);
            float PositionX = (inputsize.X - StartPosition) + (TempIndex * (StartPosition / count));
            float PositionY = inputsize.Y;
            return new Vector2(PositionX, PositionY);
        }

        /// <summary>
        /// Update at NodeObject
        /// </summary>
        /// 
        public override void UpdateTime(TimeSpan timespan)
        {
            base.UpdateTime(timespan);

            SetupPosition(MyNode.MyViewData.Position, new Vector2(MyNode.MyViewData.Source.Width, MyNode.MyViewData.Source.Height));
            NodeBody = new Rectangle((int)this.camera.ApplyPosition(this).X, (int)this.camera.ApplyPosition(this).Y, NodeBody.Width, NodeBody.Height);

        }

    }
}
